package com.shop.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.shop.bean.Goods;
import com.shop.bean.Page;
import com.shop.service.GoodsService;
import com.shop.service.impl.GoodsServiceImpl;

/**
 * Servlet implementation class GoodsServlet
 */
@WebServlet("/goods.do")
public class GoodsServlet extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		HttpSession session = req.getSession();
		if(session.getAttribute("admin") == null) {
			resp.sendRedirect("Login.jsp");
		}
		GoodsService gs = new GoodsServiceImpl();
		int pageNum = 1;
		String pageNumStr = req.getParameter("pageNum");
	    if (pageNumStr != null && !pageNumStr.isEmpty()) {
	        try {
	            pageNum = Integer.parseInt(pageNumStr);
	        } catch (NumberFormatException e) {
	            pageNum = 1;
	        }
	    }
		int pageSize = 5;
		Page<Goods> page = new Page<Goods>(gs.getGoodsByPageNS(pageNum, pageSize),pageNum,pageSize,gs.getCount());
		req.setAttribute("page", page);
		req.getRequestDispatcher("Goods.jsp").forward(req, resp);
	}

}
